/*
		ServEngine framework library
		A library to fast develop JEE online applications
	
		Copyright 2008 José Ignacio de Córdoba Álvaro

		Licensed under the Apache License, Version 2.0 (the "License");
		you may not use this file except in compliance with the License.
		You may obtain a copy of the License at

				http://www.apache.org/licenses/LICENSE-2.0

		Unless required by applicable law or agreed to in writing, software
		distributed under the License is distributed on an "AS IS" BASIS,
		WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
		See the License for the specific language governing permissions and
		limitations under the License.
	
		Jose Ignacio de Cordoba Alvaro
		http://ignacio.decordoba.com
*/

package com.servengine.entry;

import com.servengine.user.User;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Transient;

@Entity
@NamedQueries ({
	@NamedQuery(name = "Entry.findByItem", query = "select o from Entry o where o.user.portal.id = :portalid and o.module = :module and o.itemType = :itemType and o.itemId = :itemId order by o.date desc"),
	@NamedQuery(name = "Entry.findByUser", query = "select o from Entry o where o.module = :module and o.itemType = :itemType and o.user = :user order by o.date desc"),
	@NamedQuery(name = "Entry.findAfterDate", query = "select o from Entry o where o.user.portal.id = :portalid and o.module = :module and o.itemType = :itemType and o.itemId = :itemId and o.date >= :date order by o.date desc"),
	@NamedQuery(name = "Entry.itemEntryCount", query = "select count(*) from Entry o where o.user.portal.id = :portalid and o.module = :module and o.itemType = :itemType and o.itemId = :itemId"),
	@NamedQuery(name = "Entry.afterDateCount", query = "select count(*) from Entry o where o.user.portal.id = :portalid and o.module = :module and o.itemType = :itemType and o.itemId = :itemId and o.date >= :date")
})
public class Entry implements java.io.Serializable,Comparable<Entry>
{
	Integer id,itemId;
	User user;
	String userName, email;
	String module,itemType,title,body,imgurl;
	java.util.Date date;
//	boolean hasPicture,hasUserPicture;//No es CMP
	boolean privateEntry;//No es CMP
	byte[] picture;

	public Entry()
	{
	}

	public Entry(User user, String email, String userName, String module, String itemType, Integer itemId, boolean privateEntry)
	{
		this.user=user;
		if (email==null || email.length()<3)
			this.email=user.getEmail();
		else
			this.email=email;
		if (userName==null || userName.length()==0)
			setUserName(user.getName());
		else
			setUserName(userName);
		setModule(module);
		setItemType(itemType);
		setDate(new java.util.Date());
		setItemId(itemId);
		setPrivateEntry(privateEntry);
	}
	
	public int compareTo(Entry item)
	{
		int result = -date.compareTo(item.getDate());
		return result==0 ? id.compareTo(item.getId()) : result;
	}

	public boolean equals(Object o)
	{
		return ((Entry)o).getId().equals(id);
	}

	@Id @GeneratedValue
	public void setId(Integer Id)
	{
		this.id = Id;
	}
	
	@Id @GeneratedValue
	public Integer getId()
	{
		return id;
	}

	public void setTitle(String title)
	{
		this.title = title;
	}

	public String getTitle()
	{
		return title;
	}

	public void setBody(String body)
	{
		this.body = body;
	}

	@Lob
	public String getBody()
	{
		return body;
	}

	public void setImgurl(String imgurl)
	{
		this.imgurl = imgurl;
	}

	public String getImgurl()
	{
		return imgurl;
	}

	public void setModule(String module)
	{
		this.module = module;
	}

	public String getModule()
	{
		return module;
	}

	public void setItemType(String itemType)
	{
		this.itemType = itemType;
	}

	public String getItemType()
	{
		return itemType;
	}

	public void setDate(Date date)
	{
		this.date = date;
	}

	public Date getDate()
	{
		return date;
	}

	public void setItemId(Integer itemId)
	{
		this.itemId = itemId;
	}

	public Integer getItemId()
	{
		return itemId;
	}

	public void setPrivateEntry(boolean value)
	{
		privateEntry=value;
	}

	public boolean isPrivateEntry()
	{
		return privateEntry;
	}

/*	@Transient
	public void setHasUserPicture(boolean hasUserPicture)
	{
		this.hasUserPicture = hasUserPicture;
	}*/

/*	@Transient
	public boolean isHasUserPicture()
	{
		return hasUserPicture;
	}*/
/*	@Transient
	public void setHasPicture(boolean hasPicture)
	{
		this.hasPicture = hasPicture;
	}*/

	@Transient
	public boolean isHasPicture()
	{
		return picture!=null;
	}

	@ManyToOne(fetch=FetchType.EAGER, optional=false)
	public User getUser()
	{
		return user;
	}

	public void setUser(User user)
	{
		this.user = user;
	}

	public void setUserName(String userName)
	{
		this.userName = userName;
	}

	public String getUserName()
	{
		return userName;
	}

	@Transient
	public String getUserid()
	{
		return user.getUserid();
	}

	@Transient
	public Integer getUserId()
	{
		return user.getId();
	}

	public void setPicture(byte[] picture)
	{
		this.picture = picture;
	}

	@Lob
	public byte[] getPicture()
	{
		return picture;
	}

	public void setEmail(String email)
	{
		this.email = email;
	}

	public String getEmail()
	{
		return email;
	}
}
